package com.sjme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjme.domain.candidate.Candidate;
import com.sjme.mapper.CandidateMapper;
import com.sjme.domain.candidate.dto.CandidateDto;
import com.sjme.service.ICandidateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.constant.ResultConstant.ERROR;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LY
 * @since 2025-09-27
 */
@Service
public class CandidateServiceImpl extends ServiceImpl<CandidateMapper, Candidate> implements ICandidateService {
    @Autowired
    private CandidateMapper candidateMapper;

    @Override
    public IPage<Candidate> queryCandidate(CandidateDto candidate) {
        Integer current = candidate.getCurrent() == null ? 1 : candidate.getCurrent();
        Integer size = candidate.getSize() == null ? 6 : candidate.getSize();
        // 基本查询条件
        LambdaQueryWrapper<Candidate> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(candidate.getContactNumber()), Candidate::getContactNumber, candidate.getContactNumber());
        lqw.like(StringUtils.isNotBlank(candidate.getName()),Candidate::getName, candidate.getName());
        // 分页查询条件
        IPage<Candidate> page = new Page<>(current,size);
        return this.page(page, lqw);
    }

    @Override
    public Result<List<Candidate>> queryCandidateByIds(List<String> queryCandidateByIds) {
        try {
            return Result.success(this.listByIds(queryCandidateByIds));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR,"查询候选人信息异常！");
        }
    }
}
